import { _decorator, Component, Label, native, Node, Sprite, SpriteFrame, sys } from 'cc';
import { handleWifiBtn, popRoute, switchWifiBtn, routeHistory, onNative } from './Utils';
import Global from './Global';
import { mainEventTarget } from '../MainController';
import { movieEventTarget } from '../home/movie-ticket/TicketTemplate';
const { ccclass, property } = _decorator;

@ccclass('Header')
export class Header extends Component {
    @property(Node)
    back: Node | null = null;
    @property(Node)
    wifiOn: Node | null = null;
    @property(Node)
    wifiOff: Node | null = null;
    @property(Node)
    score: Node | null = null;
    @property(Node)
    setting: Node = null

    backBtnDefaultCb: Function | null = null;
    isPop: boolean = true;
    start() {

    }

    update(deltaTime: number) {
        
    }

    protected onLoad(): void {
        switchWifiBtn(this);
        if (sys.platform == sys.Platform.ANDROID) {
            onNative(this, mainEventTarget, movieEventTarget);
        }
    }
    // 监听WiFi按钮状态
    listenWifiStatus() {
        switchWifiBtn(this);
    }
    // 返回上一跳的场景， 需要在跳转前使用 loadScence(“上一场景名称”，“跳转场景名称”， 当前域this)
    touchBackEnd() {
        if (this.backBtnDefaultCb) {
            this.backBtnDefaultCb();
            if (this.isPop) {
                console.log("routeHistory", routeHistory)
                routeHistory.pop();
            }
        } else {
            popRoute();
        }
    }
    //跳wifi设置
    touchWifiEnd() {
        if (Global.wifiStatus === 'on') {
            this.wifiOn.active = true
            this.wifiOff.active = false
        } else {
            this.wifiOn.active = false
            this.wifiOff.active = true
        }
        native?.bridge?.sendToNative('wifi')
    }
    // 设置按钮
    touchSettingEnd() {
        native?.bridge?.sendToNative('settings')
    }
    // 初始化结果弹窗按钮事件
    initBtnClick(type: string, content: string) {
        console.log("header onLoad this.backBtnDefaultCb", this.backBtnDefaultCb)
        handleWifiBtn(type, content, this);
    }
    // 设置返回按钮回调
    setBackBtnDefaultCb(cb: Function) {
        this.backBtnDefaultCb = cb;
    }
    // 设置返回按钮回调
    setBackBtnDefaultCbIsPop(cb: Function, isPop: boolean) {
        this.backBtnDefaultCb = cb;
        this.isPop = isPop;
    }
    // 显示得分
    showScore(score: number, scoreIcon?: SpriteFrame) {
        this.node.getChildByName("Score").active = true;
        this.node.getChildByName("Score").getChildByName("ScoreTxt").getComponent(Label).string = "x " + score;
        if (scoreIcon) {
            this.node.getChildByName("Score").getChildByName("ScoreIcon").getComponent(Sprite).spriteFrame = scoreIcon;
        }
    }
}


